<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=8" />
    <title>Gestalt Sample: Advanced Transforms - Python</title>

    <script src="../../js/jquery.js" type="text/javascript"></script>

    <script src="../../js/gestalt.js" type="text/javascript"></script>

</head>
<body >

<script language="python">

# imports
from System import * # for Convert
from System.Windows.Shapes import * # exposes Rectangle to scope since not added by default
from System.Windows.Media.Animation import * # exposes Storyboard
from System.Windows.Media.Imaging import * # for bitmap
from System.Windows.Media import * # for ImageBrush
from System.Diagnostics import * # enables outputing to a debug window!
from System.Windows.Interop import * # for Framerate
from System.Net import * # for WebClient
from System.Windows.Resources import * # for WebClient

# localize
layoutRoot = me.LayoutRoot
tiles = me.tiles
cover = me.cover

# locals
mouse = Point(0, 0)
_file = None # equivalent to C#'s null;
webClient = WebClient()
Tiles = None

#
# define class that procedurally creates Tile code instead of XAML
#
class Tile(Canvas): # inherits from Canvas
    def __init__(self):
    
        ### CREATE INSTANCE VARIABLE ###
        self.targetAngle = 0
        layoutRoot = Canvas()
        self.Children.Add(layoutRoot)
        
        self.bkgBrushPosition = TranslateTransform()
        self.bkgBrushPosition.X = 0
        self.bkgBrushPosition.Y = 0
        
        self.bkgBrush = ImageBrush()
        self.bkgBrush.AlignmentX = AlignmentX.Left
        self.bkgBrush.AlignmentY = AlignmentY.Top
        self.bkgBrush.Stretch = Stretch.None
        self.bkgBrush.Transform = self.bkgBrushPosition
        self.bkgBrush.ImageSource = BitmapImage(Uri("../samples/transforms/images/pic3.png", UriKind.Relative))
        
        self.bkg = Rectangle()
        self.bkg.Width = 30
        self.bkg.Height = 30
        self.bkg.Fill = self.bkgBrush
        self.bkg.RenderTransformOrigin = Point (0.45,0.45)
        layoutRoot.Children.Add(self.bkg)

        self.scale = ScaleTransform()
        self.scale.ScaleX = 1.0
        self.scale.ScaleY = 1.0
        
        self.rotation = RotateTransform()
        self.rotation.Angle = 0
       
        transformGroup = TransformGroup()
        transformGroup.Children.Add(self.scale)
        transformGroup.Children.Add(self.rotation)
        self.bkg.RenderTransform = transformGroup
                   
        def sb_Completed(sender, e):
            self.rotation.Angle = self.rotation.Angle + ( self.targetAngle - self.rotation.Angle) * .13
            sb.Begin()
        
        sb = Storyboard()
        sb.Completed += sb_Completed
        layoutRoot.Resources.Add("sb",sb)
        sb.Begin()
        
    ### X property ###
    def SetX(self, value):
        self.SetValue(Canvas.LeftProperty, Convert.ToDouble(value))
    def GetX(self):
        return self.GetValue(Canvas.LeftProperty)
    X = property(GetX, SetX)
    
    ### Y property ###  
    def SetY(self, value):
        self.SetValue(Canvas.TopProperty, Convert.ToDouble(value))
    def GetY(self):
        return self.GetValue(Canvas.TopProperty)
    Y = property(GetY, SetY)
    
#
# Define event handlers before they are referenced below
#
def Page_MouseMove(sender, e):
    global mouse
    global layoutRoot
    mouse = e.GetPosition(layoutRoot)
    
def sb_Completed(sender, e):
    global tiles
    global sb
    global mouse
    for s in tiles.Children:
        _y = s.Y - mouse.Y
        _x = s.X - mouse.X
        distance = Math.Sqrt((_y * _y) + (_x * _x))
        rad = Math.Atan2(_y, _x)
        angle = rad * (180 / Math.PI)
        newScale = .3 + ((distance / 100.0));
        if newScale > 1.2:
            newScale = 1.2
        s.scale.ScaleX = s.scale.ScaleY = newScale
        if distance < 100:
            s.targetAngle = angle - 180
        else: 
            s.targetAngle = 0
    sb.Begin()

# TODO: Load up XAML instead of creating Tile Procedurally
def webClient_OpenReadCompleted(sender, e):
    global _file
    if (e.Error != None):
        txtOut.Text = "Error: " + e.Error.Message
        return
    _file = StreamResourceInfo(e.Result, None)
    #Tile = XamlReader.Load("")

#
# attach event handler
#
layoutRoot.MouseMove += Page_MouseMove
#webClient.OpenReadCompleted += webClient_OpenReadCompleted

#
# run it
#
cover.Visibility = Visibility.Visible

settings = Settings();
settings.MaxFrameRate = 30;

for x in range(0,10):
    for y in range(0, 10):
        s = Tile()
        s.X = 160 + (x * 25)
        s.Y = 25 + (y * 25)
        s.bkgBrushPosition.X = -s.X - 45;
        s.bkgBrushPosition.Y = -s.Y - 30;
        tiles.Children.Add(s)

sb = Storyboard()
sb.Completed += sb_Completed
layoutRoot.Resources.Add("sb", sb)
sb.Begin()

#webClient.OpenReadAsync(Uri("../samples/transforms/Tile.xaml", UriKind.Relative))

</script>

    <!-- This isn't a part of the sample, but a link back to the Gestalt site -->
    <div id="upsell" style="color:#fff; font-size: 13px; font-family:'Lucida Grande','Lucida Sans Unicode',Helvetica,Arial,Lucida,Verdana,sans-serif; margin-bottom: 30px;">
        <a href="http://www.visitmix.com/labs/gestalt/samples/" style="background-color: #000; color: #fff; display: block; text-decoration: none;" onmouseover="this.style.color='#000'; this.style.background='#ccc'" onmouseout="this.style.color='#fff'; this.style.background='#000'">
		    <span style="display:block; padding: 8px; text-align:center;">&laquo; Check out other samples on the Gestalt web site</span>
	    </a>
    </div>

<!-- The following commented line of code is equivalent to the inline XAML
     and we recommend doing it this way. Only caveat, if you link to a XAML
     file, you'll be able to view the page correctly only through a server 
     (e.g. apache or IIS) because of security sandbox reasons. -->
    
    <!--<xml src="page.xaml" id="page" class="xaml" width="600" height="300" style="display:none;"></xml>-->
    
    <xml id="page" class="xaml" width="600" height="300" style="display:none;" src="page.xaml">
        <UserControl 
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:xaml="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            Width="600" Height="300">
          <Canvas x:Name="LayoutRoot" Background="#222222">
            <Canvas x:Name="tiles"></Canvas>
            <!-- the xaml prefix on the Image is necessary to circumvent a XML parsing bug in FireFox and Chrome-->
            <xaml:Image x:Name="cover" Source="../samples/transforms/images/cover.png" Width="600" Height="330" Stretch="Fill" Visibility="Collapsed"></xaml:Image>
          </Canvas>
        </UserControl>
    </xml>
    
</body>
</html>
